<?php

/**
 * Created by PhpStorm.
 * User: Hasche
 * Date: 11/8/14
 * Time: 9:24 AM
 */
class GL_Product extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $data['layout_count'] = $this->product->count_pending();
        $data['layout_pending'] = $this->product->get_all_pending();
        $this->layout->setLayout("admin/layout", $data);
        if ($this->permission() != 0) redirect('../');
    }

    public function index()
    {
        //$this->layout->view("admin/index");
    }

    //Category
    public function save_category()
    {
        if ($this->input->post('id') != "") {
            $data = array('name' => $this->input->post('name'));
            echo $this->category->update($this->input->post('id'), $data);
        } else echo $this->category->save();
    }

    public function delete_category()
    {
        echo $this->category->delete($this->input->post('id'));
    }

    public function category()
    {
        $data['entries'] = $this->category->get_all();
        $this->layout->view("admin/category", $data);
    }

    //Product
    public function detail($id)
    {
        $data['product'] = $this->product->get($id);
        $data['images'] = $this->product->get_images($id);
        $data['tags'] = $this->product->get_tags($id);
        $this->layout->view("admin/product", $data);
    }

    public function update_censored()
    {
        $censored = 0;
        if ($this->input->post('censored') == 'on') {
            $censored = 1;
        }
        $data = array('censored' => $censored);
        echo $this->product->update_censored($this->input->post('id'), $data);
    }


    public function pending()
    {
        $config['base_url'] = base_url() . "admin/product/pending";
        $config['total_rows'] = $this->product->count_pending();
        $config['per_page'] = 16;

        $start = $this->uri->segment(4);
        $this->load->library("pagination", $config);
        $data['title'] = 'Pending';
        $data['pending'] = $this->product->count_pending();
        $data['all'] = $this->product->count_all();
        $data['pagination'] = $this->pagination->create_links();
        $data['products'] = $this->product->get_pending($config['per_page'], $start);
        if ($this->input->post("ajax")) {
            $data['ajax'] = true;
            $this->load->view("admin/list_product", $data);
        } else {
            $data['ajax'] = false;
            $this->layout->view("admin/list_product", $data);
        }
    }

    public function official()
    {
        $config['base_url'] = base_url() . "admin/product/official";
        $config['total_rows'] = $this->product->count_official();
        $config['per_page'] = 16;

        $start = $this->uri->segment(4);
        $this->load->library("pagination", $config);
        $data['title'] = 'Official';
        $data['pending'] = $this->product->count_pending();
        $data['all'] = $this->product->count_all();
        $data['pagination'] = $this->pagination->create_links();
        $data['products'] = $this->product->get_official($config['per_page'], $start);
        if ($this->input->post("ajax")) {
            $data['ajax'] = true;
            $this->load->view("admin/list_product", $data);
        } else {
            $data['ajax'] = false;
            $this->layout->view("admin/list_product", $data);
        }

    }

    public function all()
    {
        $config['base_url'] = base_url() . "admin/product/all";
        $config['total_rows'] = $this->product->count_all();
        $config['per_page'] = 16;

        $start = $this->uri->segment(4);
        $this->load->library("pagination", $config);
        $data['title'] = 'All';
        $data['pending'] = $this->product->count_pending();
        $data['all'] = $this->product->count_all();
        $data['pagination'] = $this->pagination->create_links();
        $data['products'] = $this->product->get_all($config['per_page'], $start);
        if ($this->input->post("ajax")) {
            $data['ajax'] = true;
            $this->load->view("admin/list_product", $data);
        } else {
            $data['ajax'] = false;
            $this->layout->view("admin/list_product", $data);
        }
    }
} 